//
//  VistorView.swift
//  weibo1
//
//  Created by changhongliang on 2017/7/21.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit
import SnapKit

class VistorView: UIView {
    //MARK: - 构造函数
    // initWithFrame 是 UIView 的指定构造函数
    // 使用纯代码构造使用的
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //Swift 建议我们要么使用纯代码，要么使用xib
    required init?(coder aDecoder: NSCoder) {
        // 导致如果使用 SB 开发，调用这个视图，会直接崩溃
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    // MARK: - 外部控制方法
    func setupVisitorInfo(imageName:String?,title:String) {
        messageLabel.text = title
        guard let imgName = imageName else {
            iconView.isHidden = false
            //播放动画
            startAnimation()
            return
        }
        homeIconView.image = UIImage(named: imgName)
        //隐藏遮盖
        iconView.isHidden = true
        
    }
    
    // MARK: - 内部控制方法
    private func startAnimation(){
        //1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //2.设置动画属性
        anim.toValue =  CGFloat(Double.pi) * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动消失
        anim.isRemovedOnCompletion = false

        //3.添加到图层
        iconView.layer.add(anim, forKey: nil)
        
    }

    
    //MARK: -懒加载控件
    fileprivate lazy var iconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    /// 遮罩图像
    fileprivate  lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    /// 小房子
    fileprivate  lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    ///消息文字
    fileprivate lazy var messageLabel:UILabel = UILabel(title:"关注一些人，回这里看看有什么惊喜")
    ///注册按钮
    lazy var registerButton:UIButton = UIButton(title: "注册", color: UIColor.orange, imageName: "common_button_white_disable")
    ///登录按钮
    lazy  var loginButton:UIButton = UIButton(title: "登录", color: UIColor.darkGray, imageName: "common_button_white_disable")
    
    
    
}



extension VistorView {
   fileprivate func setupUI(){
    addSubview(iconView)
    addSubview(maskIconView)
    addSubview(homeIconView)
    addSubview(messageLabel)
    addSubview(registerButton)
    addSubview(loginButton)
    
    //设置约束
    iconView.snp.makeConstraints { (make) in
        make.centerX.equalTo(self.snp.centerX)
        make.centerY.equalTo(self.snp.centerY).offset(-60)
    }
    
    homeIconView.snp.makeConstraints { (make) in
        make.center.equalTo(iconView.snp.center)
    }
    
    messageLabel.snp.makeConstraints { (make) in
        make.centerX.equalTo(iconView.snp.centerX)
        make.top.equalTo(iconView.snp.bottom).offset(16)
        make.width.equalTo(224)
        make.height.equalTo(36)
    }
    
    registerButton.snp.makeConstraints { (make) in
        make.left.equalTo(messageLabel.snp.left)
        make.top.equalTo(messageLabel.snp.bottom).offset(16)
        make.width.equalTo(100)
        make.height.equalTo(36)
    }
    
    loginButton.snp.makeConstraints { (make) in
        make.right.equalTo(messageLabel.snp.right)
        make.top.equalTo(registerButton.snp.top)
        make.width.equalTo(registerButton.snp.width)
        make.height.equalTo(registerButton.snp.height)
    }
    
    maskIconView.snp.makeConstraints { (make) in
        make.top.equalTo(self.snp.top)
        make.left.equalTo(self.snp.left)
        make.right.equalTo(self.snp.right)
        make.bottom.equalTo(loginButton.snp.bottom)
    
    }
    
    backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    
        
    }
}





